home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / util / blank / bserver_v14.lha / BServer_v1.4 / Sources.lha / Sources / clients / DisplayIFF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-23  |  10.2 KB  |  457 lines

  1. ; /*
  2. sc RESOPT IGNORE=73 DATA=NEAR NMINC UCHAR CONSTLIB STREQ STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTSIZE displayiff.c
  3. slink from lib:c.o displayiff.o to //Clients/DisplayIFF lib /lib/client.lib lib:sc.lib lib:amiga20.lib SC SD NOICONS
  4. delete displayiff.o
  5. quit
  6.  
  7.  DisplayIFF 1.2  (Client for BServer)
  8.  
  9.  Copyright © 1994-1995 Stefano Reksten of 3AM - The Three Amigos!!!
  10.  All rights reserved.
  11.  
  12. */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <intuition/intuition.h>
  17. #include <datatypes/pictureclass.h>
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/intuition.h>
  21. #include <proto/graphics.h>
  22. #include <proto/dos.h>
  23. #include <proto/icon.h>
  24. #include <clib/alib_protos.h>
  25. #include <string.h>
  26. #include <time.h>
  27.  
  28. #include "/include/client.h"
  29. #include "/include/bitmap/bitmap.h"
  30. #include "/include/bitmap/bitmap_pragmas.h"
  31.  
  32. #define BADFLAGS (SPRITES|VP_HIDE|GENLOCK_AUDIO|GENLOCK_VIDEO)
  33. #define FLAGMASK (~BADFLAGS)
  34. #define CAMGMASK (FLAGMASK & 0x0000FFFFL)
  35.  
  36. #define BMHDB_CMAPOK    7
  37. #define BMHDF_CMAPOK    (1 << BMHDB_CMAPOK)
  38.  
  39. char *ver = "$VER: DisplayIFF 1.2 "__AMIGADATE__;
  40.  
  41. struct IntuitionBase *IntuitionBase;
  42. struct GfxBase *GfxBase;
  43. struct Library *BitMapBase, *IconBase;
  44.  
  45. extern ULONG RangeSeed;
  46.  
  47. struct Screen *scr;
  48. struct BitMap *iffbmap;
  49. UBYTE *iffctable;
  50. ULONG color_count, displayID;
  51. char filename[128], drawername[128];
  52. BOOL domask, iff_loaded;
  53. struct BitMapHeader bmhd;
  54. BPTR iffhandle;
  55.  
  56. UWORD top, left, delay = 1;
  57. UBYTE frameptr = 0;
  58. struct BitMap *bmap, *scalebmap, *preblitbmap;
  59. struct RastPort preblitrport;
  60.  
  61. struct DisplayIDInformation *dinfo;
  62. struct BitMapScaleInfo myscaleinfo = {
  63.     NULL, NULL, NULL, 0, 0, 0, 0,
  64.     0, 0, 0, 0, 0, 0, NULL };
  65.  
  66.  
  67. BOOL GetPicName( void )
  68. {
  69. struct DiskObject *dobj;
  70. char **ttypes;
  71. char *picname;
  72. BOOL result = FALSE;
  73.  
  74. if ( IconBase = OpenLibrary( "icon.library", 0L ) )
  75.     {
  76.     if ( dobj = GetDiskObject( "DisplayIFF" ) )
  77.         {
  78.         ttypes = dobj->do_ToolTypes;
  79.             if ( picname = FindToolType( ttypes, "PICTURE" ) )
  80.                 {
  81.                 strcpy( filename, picname );
  82.                 strcpy( drawername, picname );
  83.                 *(PathPart(drawername)) = 0;
  84.                 result = TRUE;
  85.                 }
  86.  
  87.         domask = (BOOL)FindToolType( ttypes, "MASK" );
  88.  
  89.         FreeDiskObject( dobj );
  90.         }
  91.     CloseLibrary( IconBase );
  92.     }
  93. return result;
  94. }
  95.  
  96.  
  97. void DisposeIFF( void )
  98. {
  99. if ( iffbmap )
  100.     {
  101.     DisposeBitMap( iffbmap );
  102.     iffbmap = NULL;
  103.     }
  104. if ( iffctable )
  105.     {
  106.     FreeVec( iffctable );
  107.     iffctable = NULL;
  108.     }
  109. }
  110.  
  111.  
  112. BOOL ReadUncompressedFile( void )
  113. {
  114. UBYTE *address, plane;
  115. UWORD row, bytesperrow;
  116.  
  117. bytesperrow = ((bmhd.bmh_Width+15)>>4)<<1;
  118.  
  119. for ( row = 0; row < bmhd.bmh_Height; row++ )
  120.     {
  121.     for ( plane = 0; plane < bmhd.bmh_Depth; plane++ )
  122.         {
  123.         address = iffbmap->Planes[plane] + row * iffbmap->BytesPerRow;
  124.         if ( Read( iffhandle, address, bytesperrow ) != bytesperrow )
  125.             return FALSE;
  126.         }
  127.     }
  128. return TRUE;
  129. }
  130.  
  131.  
  132. BOOL ReadByteRun1( ULONG body_length )
  133. {
  134. BYTE *memory;
  135. register BYTE *source;
  136. register UBYTE plane, counter, *dest;
  137. register UWORD row, bytesperrow, rowcounter;
  138. BOOL result = FALSE;
  139.  
  140. if ( memory=(UBYTE *)AllocMem( body_length, MEMF_ANY|MEMF_CLEAR ) )
  141.     {
  142.     if ( body_length = Read( iffhandle, memory, body_length ) )
  143.         {
  144.         source = memory;
  145.  
  146.         bytesperrow = ((bmhd.bmh_Width+15)>>4)<<1;
  147.  
  148.         for ( row = 0; row < bmhd.bmh_Height; row++ )
  149.             {
  150.             for ( plane = 0; plane < bmhd.bmh_Depth; plane++ )
  151.                 {
  152.                 dest = iffbmap->Planes[plane] + row * iffbmap->BytesPerRow;
  153.                 for ( rowcounter = 0; rowcounter < bytesperrow; )
  154.                     {
  155.                     if ( *source == -128 )
  156.                         source++;
  157.                     else
  158.                     if ( *source >= 0 )
  159.                         {
  160.                         counter = *source++ + 1;
  161.                         rowcounter += counter;
  162.                         while( counter )
  163.                             {
  164.                             *dest++ = *source++;
  165.                             counter--;
  166.                             }
  167.                         }
  168.                     else
  169.                         {
  170.                         counter = 1 - *source++;
  171.                         rowcounter += counter;
  172.                         while( counter )
  173.                             {
  174.                             *dest++ = *source;
  175.                             counter--;
  176.                             }
  177.                         source++;
  178.                         }
  179.                     }
  180.                 }
  181.             }
  182.         FreeMem( memory, body_length );
  183.         result = TRUE;
  184.         }
  185.     }
  186. return result;
  187. }
  188.     
  189.  
  190. UBYTE ReadByte( void ) {
  191. UBYTE my_byte;
  192. Read( iffhandle, &my_byte, sizeof(UBYTE) );
  193. return( my_byte ); }
  194.  
  195.  
  196. ULONG ReadLong( void ) {
  197. ULONG my_long;
  198. Read( iffhandle, &my_long, sizeof(ULONG) );
  199. return( my_long ); }
  200.  
  201.  
  202. BOOL DecodeILBM( char *filename )
  203. {
  204. ULONG    total_read = 12, form_length, chunk_found, chunk_length;
  205. UWORD    n;
  206. BOOL    found_body = FALSE;
  207.  
  208. iff_loaded = FALSE;
  209.  
  210. if ( !( iffhandle = Open( filename, MODE_OLDFILE ) ) )
  211.     goto end;
  212.  
  213. if ( ReadLong()!=ID_FORM )
  214.     goto end;
  215.  
  216. form_length = ReadLong();
  217.  
  218. if ( ReadLong()!=ID_ILBM )
  219.     goto end;
  220.  
  221. do
  222.     {
  223.     chunk_found = ReadLong();
  224.     chunk_length = ReadLong();
  225.     total_read += 4 + chunk_length;
  226.  
  227.     switch( chunk_found )
  228.         {
  229.         case    ID_BMHD:
  230.             Read( iffhandle, &bmhd, sizeof(struct BitMapHeader) );
  231.             if ( bmhd.bmh_Compression > 1 )
  232.                 goto end;
  233.             if ( bmhd.bmh_Depth == 8 && domask )
  234.                 goto end;
  235.             myscaleinfo.bsi_Width = bmhd.bmh_Width;
  236.             myscaleinfo.bsi_Height = bmhd.bmh_Height;
  237.             break;
  238.         case    ID_CAMG:
  239.             displayID = ReadLong() & CAMGMASK;
  240.             if ( displayID & HAM )
  241.                 goto end;
  242.             break;
  243.         case    ID_CMAP:
  244.             color_count = chunk_length/3;
  245.             if ( displayID & EXTRA_HALFBRITE )
  246.                 color_count >>= 1;
  247.             if ( iffctable = AllocVec( chunk_length, MEMF_ANY | MEMF_CLEAR ) )
  248.             Read( iffhandle, iffctable, chunk_length );
  249.             if ( !(bmhd.bmh_Pad & BMHDF_CMAPOK) )
  250.                 for ( n = 0; n < chunk_length; n++ )
  251.                     *(iffctable+n) = (*(iffctable+n) & 0xF0) + (*(iffctable+n)>>4);
  252.             break;
  253.         case    ID_BODY:
  254.             found_body = TRUE;
  255.  
  256.             if ( !(iffbmap = CreateBitMap( bmhd.bmh_Width, bmhd.bmh_Height, bmhd.bmh_Depth + (domask?1:0) ) ) )
  257.                 goto end;
  258.             
  259.             if ( bmhd.bmh_Compression )
  260.                 iff_loaded = ReadByteRun1( chunk_length );
  261.             else
  262.                 iff_loaded = ReadUncompressedFile();
  263.  
  264.             if ( iff_loaded && domask )
  265.                 {
  266.                 register UBYTE plane, *srcaddress, *dstaddress, bmhbpr;
  267.                 register UWORD byte, row;
  268.  
  269.                 bmhbpr = ((bmhd.bmh_Width+15)>>4)<<1;
  270.                 for ( row = 0; row < bmhd.bmh_Height; row++ )
  271.                     for ( plane = 0; plane < bmhd.bmh_Depth; plane++ )
  272.                         {
  273.                         dstaddress = iffbmap->Planes[bmhd.bmh_Depth] + iffbmap->BytesPerRow * row;
  274.                         srcaddress = iffbmap->Planes[plane] + iffbmap->BytesPerRow * row;
  275.                         for ( byte = 0; byte < bmhbpr; byte++ )
  276.                             *dstaddress++ |= *srcaddress++;
  277.                         }
  278.                 }
  279.  
  280.             Close( iffhandle ); iffhandle = NULL;
  281.             break;
  282.         default:
  283.             Seek( iffhandle, chunk_length, OFFSET_CURRENT );
  284.             break;
  285.         }
  286.     }
  287.     while( !found_body && total_read < form_length );
  288.  
  289. return TRUE;
  290.  
  291. end:
  292. if ( iffhandle )
  293.     {
  294.     Close( iffhandle );
  295.     iffhandle = NULL;
  296.     }
  297. DisposeIFF();
  298. return FALSE;
  299. }
  300.  
  301.  
  302.  
  303. void AnyIFF( void )
  304. {
  305. BOOL success = FALSE;
  306. UWORD scaleN, scaleD, swidth, sheight, n, scaledw, scaledh;
  307. struct Rectangle *rect;
  308. UBYTE *address, brightness;
  309.  
  310. if ( DecodeILBM( filename ) && ( CheckAA() || bmhd.bmh_Depth <= (( displayID & EXTRA_HALFBRITE ) ? 6 : 5 )) )
  311.     {
  312.     rect = GETTXTOSCANRECT(dinfo);
  313.     displayID = DISPLAYID(dinfo) | (displayID & EXTRA_HALFBRITE) | (displayID & LACE);
  314.  
  315.     swidth = RECTANGLEWIDTH(rect);
  316.  
  317.     if ( !CheckAA() )
  318.         {
  319.         if ( bmhd.bmh_Depth > 2 && (displayID & SUPERHIRES) )
  320.             {
  321.             displayID &= ~SUPERHIRES;
  322.             swidth >>= 2;
  323.             }
  324.         else if ( bmhd.bmh_Depth > 4 && (displayID & HIRES) )
  325.             {
  326.             displayID &= ~HIRES;
  327.             swidth >>= 1;
  328.             }
  329.         }
  330.  
  331.     if ( swidth <= bmhd.bmh_Width )
  332.         swidth = (iffbmap->BytesPerRow<<3) + 1;
  333.  
  334.     sheight = RECTANGLEHEIGHT(rect);
  335.     if ( sheight <= bmhd.bmh_Height )
  336.         sheight = iffbmap->Rows + 1;
  337.  
  338.     brightness = GETBRIGHTNESS(dinfo);
  339.  
  340.     if ( bmap = CreateBitMap( bmhd.bmh_Width, bmhd.bmh_Height, bmhd.bmh_Depth + (domask?1:0) ) )
  341.         {
  342.         if ( scalebmap = CreateBitMap( bmhd.bmh_Width, bmhd.bmh_Height, bmhd.bmh_Depth + (domask?1:0) ) )
  343.             {
  344.             myscaleinfo.bsi_SrcBitMap = iffbmap;
  345.             myscaleinfo.bsi_TempBitMap = scalebmap;
  346.             myscaleinfo.bsi_DestBitMap = bmap;
  347.  
  348.             if ( preblitbmap = CreateBitMap( bmhd.bmh_Width, bmhd.bmh_Height, bmhd.bmh_Depth ) )
  349.                 {
  350.                 InitRastPort( &preblitrport );
  351.                 preblitrport.BitMap = preblitbmap;
  352.  
  353.                 if ( scr = OpenScreenTags( NULL,
  354.                     SA_DisplayID, displayID,
  355.                     SA_Width, swidth,
  356.                     SA_Height, sheight,
  357.                     SA_Depth, bmhd.bmh_Depth,
  358.                     SA_Overscan, OSCAN_TEXT,
  359.                     SA_Type, CUSTOMSCREEN,
  360.                     SA_Quiet, TRUE,
  361.                     TAG_END ) )
  362.                     {
  363.                     register struct ViewPort *vp = &(scr->ViewPort);
  364.  
  365.                     success = TRUE;
  366.  
  367.                     SpritesOff();
  368.  
  369.                     address = iffctable;
  370.                     for ( n = 0; n < color_count; n++ )
  371.                         SetRGB4( vp, n, (*address++>>4)*brightness/100, (*address++>>4)*brightness/100, (*address++>>4)*brightness/100 );
  372.  
  373.                     frameptr = 0;
  374.  
  375.                     while( STILL_BLANKING )
  376.                         {
  377.                         do {
  378.                             scaleN = RangeRand( 32 ) + 1;
  379.                             scaleD = RangeRand( 32 ) + 1;
  380.                             } while ( scaleN >= scaleD );
  381.  
  382.                         myscaleinfo.bsi_HorNum = scaleN;
  383.                         myscaleinfo.bsi_VertNum = scaleN;
  384.                         myscaleinfo.bsi_HorDen = scaleD;
  385.                         myscaleinfo.bsi_VertDen = scaleD;
  386.  
  387.                         scaledw = scaleN * bmhd.bmh_Width / scaleD;
  388.                         scaledh = scaleN * bmhd.bmh_Height / scaleD;
  389.  
  390.                         left = RangeRand( swidth - scaledw - 1);
  391.                         top = RangeRand( sheight - scaledh - 1);
  392.  
  393.                         myscaleinfo.bsi_Flags = NULL;
  394.                         if ( RangeRand( 2 ) == 0 )
  395.                             myscaleinfo.bsi_Flags |= BSIF_INVERTHOR;
  396.                         ScaleBitMap ( &myscaleinfo );
  397.  
  398.                         if ( domask )
  399.                             {
  400.                             BltBitMap( scr->RastPort.BitMap, left, top, preblitbmap, 0, 0, scaledw, scaledh, 0xC0, 0xFF, NULL );
  401.                             BltMaskBitMapRastPort( bmap, 0, 0,
  402.                                 &preblitrport, 0, 0,
  403.                                 scaledw, scaledh, (ABC|ABNC|ANBC), (PLANEPTR)bmap->Planes[bmhd.bmh_Depth] );
  404.                             WaitTOF();
  405.                             BltBitMap( preblitbmap, 0, 0,  scr->RastPort.BitMap, left, top, scaledw, scaledh, 0xC0, 0xFF, NULL );
  406.                             }
  407.                         else
  408.                             BltBitMap( bmap, 0, 0,
  409.                                 scr->RastPort.BitMap, left, top,
  410.                                 scaledw, scaledh, 0xC0, 0xFF, NULL );
  411.  
  412.                         }
  413.                     SpritesOn();
  414.                     CloseScreen( scr );
  415.                     }
  416.                 DisposeBitMap( preblitbmap );
  417.                 }
  418.             DisposeBitMap( scalebmap );
  419.             }
  420.         DisposeBitMap( bmap );
  421.         }
  422.     DisposeIFF();
  423.     }
  424.  
  425. if ( !success )
  426.     SendClientMsg( ACTION_FAILED );
  427. }
  428.  
  429.  
  430. void __main( char *line )
  431. {
  432. if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 36L ) )
  433.     {
  434.     if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library",0L ) )
  435.         {
  436.         if ( BitMapBase = OpenLibrary( "bitmap.library", 0L ) )
  437.             {
  438.             if ( dinfo = OpenCommunication() )
  439.                 {
  440.                 if ( GetPicName() )
  441.                     {
  442.                     RangeSeed = time( NULL );
  443.  
  444.                     AnyIFF();
  445.                     CloseCommunication( dinfo );
  446.                     }
  447.                 else
  448.                     SendClientMsg( ACTION_FAILED );
  449.                 }
  450.             CloseLibrary( BitMapBase );
  451.             }
  452.         CloseLibrary( (struct Library *)GfxBase );
  453.         }
  454.     CloseLibrary( (struct Library *)IntuitionBase );
  455.     }
  456. }
  457.